/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaAlterarCategoria.java
 *
 * Created on 30/11/2012, 21:43:46
 */
package gui;

import Fachada.Fachada;
import classes.Categoria;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.GeralException;
import javax.swing.JOptionPane;

/**
 *
 * @author aluno
 */
public class telaAlterarCategoria extends javax.swing.JFrame {

    private static Fachada f;

    public telaAlterarCategoria() {
        this.setLocationRelativeTo(null);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtBoxNomeCat = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        ButtonSalvarAltCat = new javax.swing.JButton();
        ButtonCancelarAltCat = new javax.swing.JButton();
        LabelCodCat = new javax.swing.JLabel();
        ButtonDeletar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel1.setText("Alterar Categoria");

        jLabel2.setText("Cod. Categoria:");

        jLabel3.setText("Nome Categoria:");

        txtBoxNomeCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBoxNomeCatActionPerformed(evt);
            }
        });

        ButtonSalvarAltCat.setText("SALVAR");
        ButtonSalvarAltCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonSalvarAltCatActionPerformed(evt);
            }
        });

        ButtonCancelarAltCat.setText("CANCELAR");
        ButtonCancelarAltCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelarAltCatActionPerformed(evt);
            }
        });

        ButtonDeletar.setText("DELETAR");
        ButtonDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonDeletarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtBoxNomeCat, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(LabelCodCat, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(ButtonSalvarAltCat, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(ButtonCancelarAltCat)
                        .addGap(18, 18, 18)
                        .addComponent(ButtonDeletar, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(92, 92, 92)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(71, 71, 71)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(LabelCodCat, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtBoxNomeCat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonCancelarAltCat, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonSalvarAltCat, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonDeletar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonSalvarAltCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonSalvarAltCatActionPerformed
        telaAlterarCategoria tlaltcat = new telaAlterarCategoria();

        try {
            f = Fachada.getInstancia();
            Categoria cat = new Categoria();
            cat.setCodCategoria(Integer.parseInt(LabelCodCat.getText()));
            cat.setNomeCategoria(txtBoxNomeCat.getText());
            txtBoxNomeCat.requestFocus();

            f.alterarCat(cat);
            telaAlterarCategoria.this.setVisible(false);
            tlaltcat.dispose();
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());

        } catch (GeralException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }

    }//GEN-LAST:event_ButtonSalvarAltCatActionPerformed

    private void txtBoxNomeCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBoxNomeCatActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtBoxNomeCatActionPerformed

    private void ButtonCancelarAltCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelarAltCatActionPerformed
        telaAlterarCategoria tlcat = new telaAlterarCategoria();
        int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Cancelar?", "Cancelar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaAlterarCategoria.this.setVisible(false);
            tlcat.dispose();
        } else {
            repaint();
        }
    }//GEN-LAST:event_ButtonCancelarAltCatActionPerformed

    private void ButtonDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonDeletarActionPerformed

        int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Deletar?", "Deletar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaAlterarCategoria tlaltcat = new telaAlterarCategoria();
            try {
                f = Fachada.getInstancia();
                Categoria cat = new Categoria();
                cat.setCodCategoria(Integer.parseInt(LabelCodCat.getText()));

                f.deletarCat(cat);

                telaAlterarCategoria.this.setVisible(false);
                tlaltcat.dispose();
            } catch (ConsultaException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (ConexaoException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());

            } catch (GeralException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        } else {
            repaint();
        }
    }//GEN-LAST:event_ButtonDeletarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new telaAlterarCategoria().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonCancelarAltCat;
    private javax.swing.JButton ButtonDeletar;
    private javax.swing.JButton ButtonSalvarAltCat;
    public javax.swing.JLabel LabelCodCat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    public javax.swing.JTextField txtBoxNomeCat;
    // End of variables declaration//GEN-END:variables
}
